//
//  ScheduleInquiryViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/31.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "FSCalendar.h"
#import "LunarFormatter.h"
#import "UserSubscribeModel.h"

#import "ScheduleInquiryBottomView.h"

#import "ScheduleInquiryViewController.h"

@interface ScheduleInquiryViewController ()
<UIGestureRecognizerDelegate,
FSCalendarDataSource,
FSCalendarDelegate>

@property (nonatomic, strong) FSCalendar *calendar;

@property (strong, nonatomic) NSDateFormatter *dateFormatter;
@property (strong, nonatomic) LunarFormatter *lunarFormatter;

@property (nonatomic, strong) ScheduleInquiryBottomView *bottomView;

@property (nonatomic, strong) UserSubscribeModel *model;

@property (nonatomic, assign) NSTimeInterval selTime;


@property (nonatomic, assign) BOOL amCanClick;
@property (nonatomic, assign) BOOL pmCanClick;

@property (nonatomic, copy) NSString *selDateStr;

@property (nonatomic, copy) NSString *resultDateStr;


@end

@implementation ScheduleInquiryViewController

- (void)dealloc {

}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)initView {
    
    self.title = @"档期";
    
    self.amCanClick = NO;
    self.pmCanClick = NO;
    
    self.view.backgroundColor = UIColorHex(0xf1f1f1);
    
    self.bottomView = [[NSBundle mainBundle] loadNibNamed:@"ScheduleInquiryBottomView"owner:nil options:nil].firstObject;

    
    [self.view addSubview:self.calendar];
    [self.view addSubview:self.bottomView];
    
    
    [self.calendar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(300);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
        make.bottom.mas_equalTo(-Bottom_SafeHeight);
    }];
    
    self.calendar.accessibilityIdentifier = @"calendar";
    self.lunarFormatter = [[LunarFormatter alloc] init];

//    [self.calendar selectDate:[Utilities getDateWithTimestamp:[Utilities getCurrentTimestamp]]];
    
    NSDateFormatter * formatter=[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    self.selDateStr = [Utilities getTimeFromTimestamp:[Utilities getCurrentTimestamp] AndFormater:formatter];
    [self request_ScheduleWithDateString:self.selDateStr];
    
    WEAKSELF
    
    self.bottomView.ScheduleInquiryBottomBlock = ^(ScheduleInquiryBottomActionType type) {
        switch (type) {
            case ScheduleInquiryBottomActionType_am:{
                
                if (weakSelf.amCanClick) {
                    weakSelf.resultDateStr = [NSString stringWithFormat:@"%@ %@",weakSelf.selDateStr,@"上午"];
                    if (weakSelf.ScheduleInquiryResultBlock) {
                        weakSelf.ScheduleInquiryResultBlock(weakSelf.resultDateStr);
                        [weakSelf.navigationController popViewControllerAnimated:YES];
                    }
                } else {
                    [SVProgressHUD showErrorWithStatus:@"已约满"];
                    [SVProgressHUD dismissWithDelay:1];
                }
                
            } break;
            case ScheduleInquiryBottomActionType_pm:{
                
                if (weakSelf.pmCanClick) {
                    weakSelf.resultDateStr = [NSString stringWithFormat:@"%@ %@",weakSelf.selDateStr,@"下午"];
                    if (weakSelf.ScheduleInquiryResultBlock) {
                        weakSelf.ScheduleInquiryResultBlock(weakSelf.resultDateStr);
                        [weakSelf.navigationController popViewControllerAnimated:YES];
                    }
                    
                } else {
                    [SVProgressHUD showErrorWithStatus:@"已约满"];
                    [SVProgressHUD dismissWithDelay:1];
                }
                
            } break;
            default:
                break;
        }
    };
}


#pragma mark - <FSCalendarDelegate>

- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition {
    
    self.selTime = [Utilities getTimestampWithDate:date];

    NSDateFormatter * formatter=[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    self.selDateStr = [Utilities getTimeFromTimestamp:self.selTime AndFormater:formatter];
    [self request_ScheduleWithDateString:self.selDateStr];

}

//农历
- (NSString *)calendar:(FSCalendar *)calendar subtitleForDate:(NSDate *)date {
    
    return [self.lunarFormatter stringFromDate:date];
}

- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar {
    NSLog(@"%s %@", __FUNCTION__, [self.dateFormatter stringFromDate:calendar.currentPage]);
    
}


- (FSCalendar *)calendar {
    if (!_calendar) {
        _calendar = [[FSCalendar alloc] init];
        _calendar.backgroundColor = UIColorHex(0xffffff);
        _calendar.delegate = self;
        _calendar.dataSource = self;
        _calendar.appearance.weekdayTextColor = UIColorHex(0x999999);
        _calendar.appearance.headerTitleColor = UIColorHex(0x666666);
        _calendar.appearance.todayColor = UIColorHex(0xff905b);
        _calendar.appearance.selectionColor = UIColorHex(0xff4342);
        _calendar.appearance.headerDateFormat = @"yyyy-MM";
    }
    return _calendar;
}

- (void)request_ScheduleWithDateString:(NSString *)dateString {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:dateString forKey:@"unSubscribeTime"];
    [dic setValue:self.packageId forKey:@"packageId"];
    [dic setValue:self.sellerId forKey:@"sellerId"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_HallSchedule];
    
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在查询档期"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                
                weakSelf.model = [UserSubscribeModel modelWithDictionary:resultDic];
                
                switch (weakSelf.model.subscribeType) {
                    case 1:{
                    
                        weakSelf.bottomView.amLab.text = @"上午：已满";
                        weakSelf.amCanClick = NO;
                   } break;
                     case 2:{
                        weakSelf.bottomView.amLab.text = @"上午：可预约";
                         weakSelf.amCanClick = YES;

                    } break;
                    default:
                        break;
                }
                
                switch (weakSelf.model.afternoonType) {
                    case 1:{
                        weakSelf.bottomView.pmLab.text = @"下午：已满";
                        weakSelf.pmCanClick = NO;

                   } break;
                     case 2:{
                         weakSelf.bottomView.pmLab.text = @"下午：可预约";
                         weakSelf.pmCanClick = YES;
                    } break;
                    default:
                        break;
                }
                
            } else {
                weakSelf.bottomView.amLab.text = @"未获取";
                weakSelf.bottomView.pmLab.text = @"未获取";
                weakSelf.amCanClick = NO;
                weakSelf.pmCanClick = NO;
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];

            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
    }];
    
}

@end
